package com.woan.api.action;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.oreilly.servlet.MultipartRequest;
import com.woan.api.model.SysMessage;
import com.woan.api.model.SysUpgrade;
import com.woan.api.model.SysVideo;
import com.woan.api.model.ZbConfig;
import com.woan.api.model.ZbControl;
import com.woan.api.services.MessageServerServices;
import com.woan.api.services.PhotoServerServices;
import com.woan.api.services.UpgradeServerServices;
import com.woan.api.services.UserServerServices;
import com.woan.api.services.VideoServerServices;
import com.woan.api.services.ZbconfigServerServices;
import com.woan.api.services.ZbcontrolServerServices;
import com.woan.api.util.DateJsonValueProcessor;
import com.woan.api.vo.Json;
import com.woan.api.vo.VCustomvideoList;
import com.woan.api.vo.VOnevideo;
import com.woan.api.vo.VadmininfoSimpleList;
import com.woan.api.vo.VmessageList;
import com.woan.api.vo.VphotoList;
import com.woan.api.vo.VpositionList;
import com.woan.api.vo.VvideoList;
import com.woan.api.util.ConfigUtil;
import com.woan.api.util.CopyFile;
import com.woan.api.util.FuncDate;
import com.woan.api.util.Randoms;
import com.woan.api.util.UploadUtil;

public class XzbIPadApi extends ActionSupport {

	// 返回json内容
	public void reuturnjson(String json) throws IOException {

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setHeader("Cache-control", "no-store");// HTTP 1.0
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setCharacterEncoding("utf-8");// 设置编码
		PrintWriter out;
		out = response.getWriter();
		out.println(json);
		out.flush();
		out.close();
	}

	// 系统登录判断
	public void logininfo() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间

		request = ServletActionContext.getRequest();// 初始request
		Json json = new Json();
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String devicetoken = request.getParameter("devicetoken");
		String devicetype = request.getParameter("devicetype");
		String devicesys = request.getParameter("devicesys");
		String pushtype = request.getParameter("pushtype");
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		if (json.getResults().equals("0")) {
			userServerServices.saveMobileInfo(json.getAdmin().getUid(), devicenum, devicetoken, devicesys, devicetype, pushtype);
		}
		JSONObject rsjson = new JSONObject();
		rsjson.accumulate("results", Integer.parseInt(json.getResults()));
		if (json.getItems() == null)
			rsjson.accumulate("items", "");
		else
			rsjson.accumulate("items", json.getItems());
		rsjson.accumulate("errMsg", json.getErrMsg());
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("PAD登陆程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 获取组内人员
	public void listgroupuser() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<VadmininfoSimpleList> admininfo = null;
			admininfo = userServerServices.listGroupUser(json.getAdmin().getSysRole().getRid());
			if (admininfo.size() > 0) {

				JSONArray jsonArray = JSONArray.fromObject(admininfo);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到结果！");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("获取组内人员程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 直播列表
	public void listlivevideos() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String pageid = request.getParameter("pageid");
		String pagesize = request.getParameter("pagesize");
		if (pageid == null || "".equals(pageid)) {
			pageid = "1";
		}
		if (pagesize == null || "".equals(pagesize)) {
			pagesize = "10";
		}
		Integer beginIndex = (Integer.parseInt(pageid) - 1) * Integer.parseInt(pagesize);
		Integer endIndex = Integer.parseInt(pagesize);
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<VvideoList> video = null;
			video = videoServerServices.listLiveVideos(json.getAdmin().getSysRole().getRid(), json.getMobile().getPushType(), beginIndex, endIndex);
			if (video.size() > 0) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "当前没有直播视频！");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("PAD直播列表程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 历史视频列表
	public void listhistoryvideos() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String pageid = request.getParameter("pageid");
		String pagesize = request.getParameter("pagesize");
		if (pageid == null || "".equals(pageid)) {
			pageid = "1";
		}
		if (pagesize == null || "".equals(pagesize)) {
			pagesize = "10";
		}
		Integer beginIndex = (Integer.parseInt(pageid) - 1) * Integer.parseInt(pagesize);
		Integer endIndex = Integer.parseInt(pagesize);
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<VvideoList> video = null;
			video = videoServerServices.listHistoryVideos(json.getAdmin().getSysRole().getRid(), json.getMobile().getPushType(), beginIndex, endIndex);
			if (video!=null&&video.size() > 0) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到结果！");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("历史列表程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 根据id获取视频信息
	public void getvideoinfo() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String videoid = request.getParameter("videoid");
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			VOnevideo video = null;
			System.out.println(json.getMobile().getPushType());
			video = videoServerServices.listVideosByVid(videoid, json.getMobile().getPushType());
			if (video != null) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到结果！");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("根据id获取视频信息程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 照片列表
	public void listphotos() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String pageid = request.getParameter("pageid");
		String pagesize = request.getParameter("pagesize");
		if (pageid == null || "".equals(pageid)) {
			pageid = "1";
		}
		if (pagesize == null || "".equals(pagesize)) {
			pagesize = "10";
		}
		Integer beginIndex = (Integer.parseInt(pageid) - 1) * Integer.parseInt(pagesize);
		Integer endIndex = Integer.parseInt(pagesize);
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<VphotoList> photo = null;
			photo = photoServerServices.listPhotos(json.getAdmin().getSysRole().getRid(), beginIndex, endIndex);
			if (photo!=null&&photo.size() > 0) {
				for (VphotoList vphoto : photo) {
					vphoto.setSmurl(ConfigUtil.get("serverinfo")+vphoto.getSmurl());
					vphoto.setUrl(ConfigUtil.get("serverinfo")+vphoto.getUrl());
				}
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(photo, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到结果！");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("PAD照片列表程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 信息列表
	public void listmessage() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String pageid = request.getParameter("pageid");
		String pagesize = request.getParameter("pagesize");
		if (pageid == null || "".equals(pageid)) {
			pageid = "1";
		}
		if (pagesize == null || "".equals(pagesize)) {
			pagesize = "10";
		}
		Integer beginIndex = (Integer.parseInt(pageid) - 1) * Integer.parseInt(pagesize);
		Integer endIndex = Integer.parseInt(pagesize);
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<VmessageList> message = messageServerServices.listHistoryMessage(json.getAdmin().getSysRole().getRid(), beginIndex, endIndex);
			if (message.size() > 0) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(message, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未查到结果！");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("PAD历史消息程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 发送文字消息
	public void sendmessage() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String message = request.getParameter("message");
		FuncDate fd = new FuncDate();
		SysMessage sysmessage = new SysMessage();
		sysmessage.setMid(fd.FindCurSysTime() + Randoms.getRandomString(3));
		sysmessage.setMsgText(message);
		sysmessage.setSendtime(fd.getSysTime());
		sysmessage.setIsdel("1");
		sysmessage.setMsgType("1");
		sysmessage.setUserName(username);
		sysmessage.setVoicelength(0);
		sysmessage.setMsg_web(message);
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			rsjson = messageServerServices.sendMessage(groupname, username, userpwd, sysmessage);
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());

		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("PAD发送消息程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 设置直播页面
	public void configlivevideos() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String config = request.getParameter("config");
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			rsjson = zbconfigServerServices.saveVideoConfig(json.getAdmin().getSysRole().getRid(), config);
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("配置ipad播放页面程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 获取直播设置页面
	public void listconfig() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request

		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<ZbConfig> zbconfig = null;
			zbconfig = zbconfigServerServices.listConfig(json.getAdmin().getSysRole().getRid());
			if (zbconfig!=null&&zbconfig.size() > 0) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(zbconfig, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "没有历史视频！");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("获取ipad播放设置页面程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 自定义播放列表
	public void listconfiglivevideos() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		Integer pagesize = 4;
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<VCustomvideoList> video = null;
			video = zbconfigServerServices.listConfigLiveVideos(json.getAdmin().getSysRole().getRid());
			// 查询自定义列表
			if (video!=null&&video.size() > 0) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "当前没有直播视频！");

			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("自定义播放列表程序运行时间： " + (endTime - startTime) + "ms");
	}

	// //获取历史视频页数，总数
	// public void gethistoryvideospage() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// request = ServletActionContext.getRequest();// 初始request
	// JSONObject json=new JSONObject();
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// //获取每页显示的数量
	// Integer pagenum = Integer.parseInt(request.getParameter("pagenum"));
	// //获取总数
	// Integer total = videoServerServices.listHistoryVideosCount(groupname, username, userpwd);
	// //计算页数
	// Integer pagecount = 0;
	// if(pagenum>0)
	// if(total%pagenum>0)
	// pagecount = total/pagenum+1;
	// else
	// pagecount = total/pagenum;
	// else
	// pagecount = 0;
	// if(total>0)
	// {
	//
	// json.accumulate("results", 1);
	// json.accumulate("items", pagecount);
	// json.accumulate("errMsg", "查到结果！");
	// }
	// else
	// {
	// json.accumulate("results", 0);
	// json.accumulate("items", "");
	// json.accumulate("errMsg", "没有历史视频！");
	// }
	// reuturnjson(json.toString());
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("获取历史视频页数，总数程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// //编辑视频
	// public void editvideos() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// JSONObject json=new JSONObject();
	// request = ServletActionContext.getRequest();// 初始request
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// String videodetail = request.getParameter("videodetail");
	// String videoid = request.getParameter("videoid");
	// if(videoid != null && !"".equals(videoid)){
	// json = videoServerServices.editVideos(groupname, username,userpwd,devicenum,videoid,videodetail);
	// reuturnjson(json.toString());
	// }
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("编辑视频程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// //删除视频，支持单个删除和批量删除
	// public void deletevideos() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// JSONObject json=new JSONObject();
	// request = ServletActionContext.getRequest();// 初始request
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// String videoid = request.getParameter("videoid");
	// if(videoid != null && !"".equals(videoid)){
	// json = videoServerServices.deleteVideos(groupname, username,userpwd,devicenum,videoid);
	// reuturnjson(json.toString());
	// }
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("删除视频程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// //根据视频描述关键字模糊搜索视频
	// public void searchvideos() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// Map<String, String> map = new HashMap<String, String>();
	// Integer pagesize=10;
	// JSONObject json=new JSONObject();
	// request = ServletActionContext.getRequest();// 初始request
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// String pageid = request.getParameter("pageid");
	// if(pageid == null || "".equals(pageid)){
	// pageid = "1";
	// }
	// Integer beginIndex = (Integer.parseInt(pageid)-1) * pagesize;
	// Integer endIndex = pagesize;
	// //视频描述
	// String detail = request.getParameter("detail");
	// if(detail!=null)
	// map.put("detail", detail);
	// //视频标题
	// String title = request.getParameter("title");
	// if(title!=null)
	// map.put("title", title);
	// //拍摄人
	// String paishename = request.getParameter("paishename");
	// if(paishename!=null)
	// map.put("paishename", paishename);
	//
	// List<VvideoList> video = null;
	//
	// video = videoServerServices.searchVideos(groupname, username,userpwd,devicenum, beginIndex, endIndex,map);
	//
	// if(video.size()>0)
	// {
	// JsonConfig jsonConfig = new JsonConfig();
	// jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
	// JSONArray jsonArray = JSONArray.fromObject(video, jsonConfig);
	// json.accumulate("results", 1);
	// json.accumulate("items", jsonArray.toString());
	// json.accumulate("errMsg", "查到结果！");
	// }
	// else
	// {
	// json.accumulate("results", 0);
	// json.accumulate("items", "");
	// json.accumulate("errMsg", "未查到数据！");
	// }
	// reuturnjson(json.toString());
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("搜索视频程序运行时间： "+(endTime-startTime)+"ms");
	// }
	//
	// //获取照片页数，总数
	// public void getphotospage() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// request = ServletActionContext.getRequest();// 初始request
	// JSONObject json=new JSONObject();
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// //获取每页显示的数量
	// Integer pagenum = Integer.parseInt(request.getParameter("pagenum"));
	// //获取总数
	// Integer total = photoServerServices.listPhotosCount(groupname, username, userpwd);
	// //计算页数
	// Integer pagecount = 0;
	// if(pagenum>0)
	// if(total%pagenum>0)
	// pagecount = total/pagenum+1;
	// else
	// pagecount = total/pagenum;
	// else
	// pagecount = 0;
	// if(total>0)
	// {
	//
	// json.accumulate("results", 1);
	// json.accumulate("items", pagecount);
	// json.accumulate("errMsg", "查到结果！");
	// }
	// else
	// {
	// json.accumulate("results", 0);
	// json.accumulate("items", "");
	// json.accumulate("errMsg", "没有照片！");
	// }
	// reuturnjson(json.toString());
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("获取照片页数，总数程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// //搜索照片
	// public void searchphotos() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// Map<String, String> map = new HashMap<String, String>();
	// Integer pagesize=10;
	// request = ServletActionContext.getRequest();// 初始request
	// JSONObject json=new JSONObject();
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// //照片发布人
	// String fabuname = request.getParameter("fabuname");
	// if(fabuname!=null)
	// map.put("fabuname", fabuname);
	// //照片描述
	// String title = request.getParameter("title");
	// if(title!=null)
	// map.put("title", title);
	// //开始时间
	// String begintime = request.getParameter("begintime");
	// if(begintime!=null)
	// map.put("begintime", begintime);
	// //结束时间
	// String endtime = request.getParameter("endtime");
	// if(endtime!=null)
	// map.put("endtime", endtime);
	// String pageid = request.getParameter("pageid");
	// if(pageid == null || "".equals(pageid)){
	// pageid = "1";
	// }
	// Integer beginIndex = (Integer.parseInt(pageid)-1) * pagesize;
	// Integer endIndex = pagesize;
	// List<VphotoList> photo = null;
	// photo = photoServerServices.FindVSerachPhotoByCon(groupname, username, userpwd, beginIndex, endIndex, map);
	// if(photo.size()>0)
	// {
	// JsonConfig jsonConfig = new JsonConfig();
	// jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
	// JSONArray jsonArray = JSONArray.fromObject(photo, jsonConfig);
	// json.accumulate("results", 1);
	// json.accumulate("items", jsonArray.toString());
	// json.accumulate("errMsg", "查到结果！");
	// }
	// else
	// {
	// json.accumulate("results", 0);
	// json.accumulate("items", "");
	// json.accumulate("errMsg", "未查到数据！");
	// }
	// reuturnjson(json.toString());
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("搜索照片程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// //编辑照片
	// public void editphotos() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// JSONObject json=new JSONObject();
	// request = ServletActionContext.getRequest();// 初始request
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// String photoid = request.getParameter("photoid");
	// String title = request.getParameter("title");
	// if(photoid != null && !"".equals(photoid)){
	// json = photoServerServices.editPhotos(groupname, username, userpwd, photoid, title);
	// reuturnjson(json.toString());
	// }
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("编辑照片程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// //删除视频，支持单个删除和批量删除
	// public void deletephotos() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// JSONObject json=new JSONObject();
	// request = ServletActionContext.getRequest();// 初始request
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// String photoid = request.getParameter("photoid");
	// if(photoid != null && !"".equals(photoid)){
	// json = photoServerServices.deletePhotos(groupname, username, userpwd, devicenum, photoid);
	// reuturnjson(json.toString());
	// }
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("删除照片程序运行时间： "+(endTime-startTime)+"ms");
	// }
	//
	//
	// //搜索信息
	// public void searchmessage() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// Map<String, String> map = new HashMap<String, String>();
	// Integer pagesize=10;
	// request = ServletActionContext.getRequest();// 初始request
	// JSONObject json=new JSONObject();
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// //信息发布人
	// String fabuname = request.getParameter("fabuname");
	// if(fabuname!=null)
	// map.put("fabuname", fabuname);
	// //开始时间
	// String begintime = request.getParameter("begintime");
	// if(begintime!=null)
	// map.put("begintime", begintime);
	// //结束时间
	// String endtime = request.getParameter("endtime");
	// if(endtime!=null)
	// map.put("endtime", endtime);
	// String pageid = request.getParameter("pageid");
	// if(pageid == null || "".equals(pageid)){
	// pageid = "1";
	// }
	// Integer beginIndex = (Integer.parseInt(pageid)-1) * pagesize;
	// Integer endIndex = pagesize;
	// List<VmessageList> message = messageServerServices.searchMessage(groupname, username, userpwd, devicenum, beginIndex, endIndex, map);
	// if(message.size()>0)
	// {
	// JsonConfig jsonConfig = new JsonConfig();
	// jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
	// JSONArray jsonArray = JSONArray.fromObject(message, jsonConfig);
	// json.accumulate("results", 1);
	// json.accumulate("items", jsonArray.toString());
	// json.accumulate("errMsg", "查到结果！");
	// }
	// else
	// {
	// json.accumulate("results", 0);
	// json.accumulate("items", "");
	// json.accumulate("errMsg", "未查到数据！");
	// }
	// reuturnjson(json.toString());
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("搜索信息程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// //删除信息，支持单个删除和批量删除
	// public void deletemessage() throws IOException{
	// long startTime=System.currentTimeMillis(); //获取开始时间
	// JSONObject json=new JSONObject();
	// request = ServletActionContext.getRequest();// 初始request
	// String groupname = request.getParameter("groupname");
	// String username = request.getParameter("username");
	// String userpwd = request.getParameter("userpwd");
	// String devicenum = request.getParameter("devicenum");
	// String messageid = request.getParameter("messageid");
	// if(messageid != null && !"".equals(messageid)){
	// json = messageServerServices.deleteMessage(groupname, username, userpwd, devicenum, messageid);
	// reuturnjson(json.toString());
	// }
	// long endTime=System.currentTimeMillis(); //获取结束时间
	// System.out.println("删除信息程序运行时间： "+(endTime-startTime)+"ms");
	// }
	// 位置信息
	public void baiduposition() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<VpositionList> position = videoServerServices.listVideoPostion(json.getAdmin().getSysRole().getRid(), json.getMobile().getPushType());
			if (position.size() > 0) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(position, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未获取到信息");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("位置信息程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 对讲模式--获取信息
	public void listvideotalk() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String videoid = request.getParameter("videoid");
		Json json = new Json();
		json = userServerServices.loginInfo(groupname, username, userpwd, devicenum);
		JSONObject rsjson = new JSONObject();
		if (json.getResults().equals("0")) {
			List<ZbControl> position = zbcontrolServerServices.listVideoTalkMessage(videoid);
			if (position.size() > 0) {
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor());
				JSONArray jsonArray = JSONArray.fromObject(position, jsonConfig);
				rsjson.accumulate("results", 0);
				rsjson.accumulate("items", jsonArray.toString());
				rsjson.accumulate("errMsg", "查到结果！");
			} else {
				rsjson.accumulate("results", 1);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "未获取到信息");
			}
		} else {
			rsjson.accumulate("results", 2);
			rsjson.accumulate("errMsg", "用户信息错误！");
		}
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("PAD对讲信息获取程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 对讲模式--发送文字信息
	public void sendvideotalk() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		System.out.println("发送文字消息开始了！");
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String videoid = request.getParameter("videoid");
		String msgtext = request.getParameter("msgtext");
		String touser = request.getParameter("touser");
		FuncDate fd = new FuncDate();
		ZbControl zbcontrol = new ZbControl();
		zbcontrol.setContent(msgtext);
		// 控制类型，1为文字，0为语音
		zbcontrol.setContentType("1");
		zbcontrol.setCreateTime(fd.getSysTime());
		zbcontrol.setVideoId(videoid);
		zbcontrol.setFromuser(username);
		zbcontrol.setTouser(touser);
		JSONObject json = new JSONObject();
		json = zbcontrolServerServices.sendMessage(request, groupname, username, userpwd, zbcontrol, touser);
		reuturnjson(json.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("发送对讲消息程序运行时间： " + (endTime - startTime) + "ms");
	}

	// 系统升级
	public void upgrade() throws IOException {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		request = ServletActionContext.getRequest();// 初始request
		String groupname = request.getParameter("groupname");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String devicenum = request.getParameter("devicenum");
		String os = request.getParameter("os");
		String vername = request.getParameter("vername");
		String vercode = request.getParameter("vercode");
		// Json json = new Json();
		// json = userServerServices.loginInfo(groupname, username,userpwd,devicenum);
		JSONObject rsjson = new JSONObject();
		// if(json.getResults().equals("0"))
		// {
		SysUpgrade sysupgrade = upgradeServerServices.listNewVersion(groupname, username, userpwd, os, vername);
		if (sysupgrade != null && sysupgrade.getVersioncode() != null) {
			String items = JSONObject.fromObject(sysupgrade).toString();
			String[] comcode = vercode.split("\\.");
			String[] curcode = sysupgrade.getVersioncode().split("\\.");
			if (comcode.length != 3) {
				rsjson.accumulate("results", 10);
				rsjson.accumulate("items", "");
				rsjson.accumulate("errMsg", "客户端非法版本号！");
			} else {
				if (Integer.parseInt(curcode[0]) > Integer.parseInt(comcode[0])) {
					rsjson.accumulate("results", 0);
					rsjson.accumulate("items", items);
					rsjson.accumulate("errMsg", "");
				} else if (Integer.parseInt(curcode[0]) == Integer.parseInt(comcode[0])) {
					if (Integer.parseInt(curcode[1]) > Integer.parseInt(comcode[1])) {
						rsjson.accumulate("results", 0);
						rsjson.accumulate("items", items);
						rsjson.accumulate("errMsg", "");
					} else if (Integer.parseInt(curcode[1]) == Integer.parseInt(comcode[1])) {
						if (Integer.parseInt(curcode[2]) > Integer.parseInt(comcode[2])) {
							rsjson.accumulate("results", 0);
							rsjson.accumulate("items", items);
							rsjson.accumulate("errMsg", "");
						} else {
							rsjson.accumulate("results", 1);
							rsjson.accumulate("items", "");
							rsjson.accumulate("errMsg", "未获取到版本信息");
						}
					} else {
						rsjson.accumulate("results", 1);
						rsjson.accumulate("items", "");
						rsjson.accumulate("errMsg", "未获取到版本信息");
					}
				} else {
					rsjson.accumulate("results", 1);
					rsjson.accumulate("items", "");
					rsjson.accumulate("errMsg", "未获取到版本信息");
				}
			}
		} else {
			rsjson.accumulate("results", 1);
			rsjson.accumulate("items", "");
			rsjson.accumulate("errMsg", "未获取到版本信息");
		}
		// }
		// else
		// {
		// rsjson.accumulate("results", 2);
		// rsjson.accumulate("errMsg", "用户信息错误！");
		// }
		reuturnjson(rsjson.toString());
		long endTime = System.currentTimeMillis(); // 获取结束时间
		System.out.println("系统升级程序运行时间： " + (endTime - startTime) + "ms");
	}

	private HttpServletRequest request;
	private HttpServletResponse response;
	private UserServerServices userServerServices;
	private VideoServerServices videoServerServices;
	private PhotoServerServices photoServerServices;
	private MessageServerServices messageServerServices;
	private ZbcontrolServerServices zbcontrolServerServices;
	private ZbconfigServerServices zbconfigServerServices;
	private UpgradeServerServices upgradeServerServices;

	public ZbconfigServerServices getZbconfigServerServices() {
		return zbconfigServerServices;
	}

	public void setZbconfigServerServices(ZbconfigServerServices zbconfigServerServices) {
		this.zbconfigServerServices = zbconfigServerServices;
	}

	public MessageServerServices getMessageServerServices() {
		return messageServerServices;
	}

	public void setMessageServerServices(MessageServerServices messageServerServices) {
		this.messageServerServices = messageServerServices;
	}

	public PhotoServerServices getPhotoServerServices() {
		return photoServerServices;
	}

	public void setPhotoServerServices(PhotoServerServices photoServerServices) {
		this.photoServerServices = photoServerServices;
	}

	public UserServerServices getUserServerServices() {
		return userServerServices;
	}

	public void setUserServerServices(UserServerServices userServerServices) {
		this.userServerServices = userServerServices;
	}

	public VideoServerServices getVideoServerServices() {
		return videoServerServices;
	}

	public void setVideoServerServices(VideoServerServices videoServerServices) {
		this.videoServerServices = videoServerServices;
	}

	public ZbcontrolServerServices getZbcontrolServerServices() {
		return zbcontrolServerServices;
	}

	public void setZbcontrolServerServices(ZbcontrolServerServices zbcontrolServerServices) {
		this.zbcontrolServerServices = zbcontrolServerServices;
	}

	public UpgradeServerServices getUpgradeServerServices() {
		return upgradeServerServices;
	}

	public void setUpgradeServerServices(UpgradeServerServices upgradeServerServices) {
		this.upgradeServerServices = upgradeServerServices;
	}

}
